<script setup>
import { ref } from "vue";
import { PlusCircleFilled, CloseCircleOutlined } from "@ant-design/icons-vue";
import request from "@/common/utils/request";
import utils from "@/common/utils/utils";
import { message } from "ant-design-vue";
import addArticlePublish from "@/components/ArticlePublish/addArticlePublish.vue";
import { watch } from "vue";
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  accounts: {
    type: Array,
    default: () => [],
  },
  groupName: {
    type: String,
    default: "",
  },
  editId: {
    type: [String, Number],
    default: "",
  },
  isShowAccount: {
    //是否显示账号字段
    type: Boolean,
    default: true,
  },
});

const emit = defineEmits(["update:visible", "created", "clear"]);
let group_mame = ref("");
let accountVisible = ref(false);
let chooseAccounts = ref([]);

const onClose = () => {
  emit("update:visible", false);
};
const ok = (account) => {
  chooseAccounts.value = account;
  accountVisible.value = false;
};
const closeAccountModal = () => {
  accountVisible.value = true;
};
const delAccount = (index) => {
  chooseAccounts.value.splice(index, 1);
};
const submit = async () => {
  if (!group_mame.value) {
    message.error("请输入分组名称");
    return;
  }
  let account_id = chooseAccounts.value.map((item) => item.id);
  let params = {
    title: group_mame.value,
  };
  if (account_id.length) {
    params.account_id = account_id;
  }
  let method = "post";
  let url = "/org/platformAccount/group/add";
  if (props.editId) {
    params.id = props.editId;
    method = "put";
    url = "/org/platformAccount/group/update";
  }

  await request[method]("/work_ai", url, params);
  message.success(`${props.editId ? "编辑成功" : "创建成功"}`);
  chooseAccounts.value = [];
  group_mame.value = "";
  emit("created");
  emit("update:visible", false);
};
watch(
  () => props.visible,
  (val) => {
    if (val) {
      group_mame.value = props.groupName;
      chooseAccounts.value = props.accounts.length ? props.accounts : [];
    } else {
      emit("clear");
    }
  }
);
</script>

<template>
  <div>
    <a-modal
      :visible="visible"
      :title="`${editId ? '编辑分组' : '创建分组'}（账号可在多个分组内）`"
      centered
      :width="500"
      @cancel="onClose"
      @ok="submit"
      :body-style="{
        padding: '10px 24px',
      }"
    >
      <div class="modal-content">
        <div class="text-gray-6 mb-20">
          分组信息用于“一键发布”-“快捷添加分组”、“账号中心”-“分组筛选”；可按分类/领域、归属主体、平台、特殊权益等方式组合命名。
          (参考命名：科技类1-抖音-自营)
        </div>
        <a-form>
          <a-form-item label="分组" required>
            <a-input v-model:value.trim="group_mame"></a-input>
          </a-form-item>
          <a-form-item label="账号" v-if="isShowAccount">
            <div class="operation-area">
              <div
                class="account-item"
                v-for="(item, index) in chooseAccounts"
                :key="item.id"
              >
                <close-circle-outlined
                  class="close-icon"
                  @click="delAccount(index)"
                />

                <a-tooltip>
                  <template #title>{{ item.nickname }}</template>
                  <div class="user-avar">
                    <img :src="item.avatar" class="user-icon" />
                    <img :src="item.platform.image" class="platform-icon" />
                  </div>
                </a-tooltip>
              </div>

              <div class="add-btn" @click="closeAccountModal">
                <plus-circle-filled style="color: #e0e0e6; font-size: 30px" />
                <span class="text-gray-6">添加账号</span>
              </div>
            </div>
          </a-form-item>
        </a-form>
      </div>
    </a-modal>
    <Suspense>
      <addArticlePublish
        :visible="accountVisible"
        v-if="accountVisible"
        :account="chooseAccounts"
        @handleOk="ok"
        @onClose="
          () => {
            accountVisible = false;
          }
        "
      ></addArticlePublish>
    </Suspense>
  </div>
</template>

<style lang="less" scoped>
.add-btn {
  display: flex;
  flex-direction: column;
  width: 64px;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  margin-top: 8px;
}
.operation-area {
  display: flex;

  flex-wrap: wrap;
}
.account-list {
  display: flex;
  flex-wrap: wrap;
}
.account-item {
  position: relative;
  display: flex;
  flex-direction: column;

  align-items: center;
  margin: 8px;
}
.close-icon {
  position: absolute;
  top: -8px;
  right: 0;
  font-size: 12px;
  z-index: 2;
  visibility: hidden;
  cursor: pointer;
}
.user-avar {
  position: relative;
  width: 40px;
  height: 40px;
  cursor: pointer;
}
.account-item:hover .close-icon {
  visibility: visible;
}
.user-icon {
  position: absolute;
  width: 40px;
  height: 40px;
  border: 1px solid #000;
  border-radius: 50%;
}
.platform-icon {
  position: absolute;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  bottom: 0;
  right: 0;
  z-index: 1;
}
.user-name {
  width: 80px;
  text-align: center;
}
</style>
